package com.rich.common.util;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;/**
 * @Package: com.rich.common.util
 * @Author: Lin
 * @Date: Created in 2021/8/26 10:49
 */

/**
 * @program: richhouse
 *
 * @author: Mr.Lin
 *
 * @create: 2021-08-26 10:49
 **/
public class SendSms {
    public static void main(String[] args) {
        SendSms.sendSms("18210662334","dongfangshuye");
    }
    //发送短信方法
    public static void sendSms(String phone ,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5t6UJzWhqGnu6LrN7kn9", "x1cN77D3wZCAwOceopGn1aiYip7LD4");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "郭帅公众号签名");
        request.putQueryParameter("TemplateCode", "SMS_165386007");
        // 在字符串中 ” 需要转义 \" 代表 "
        // {"code":"gouzhen"}  ==>  "{\"code\":\"gouzhen\"}" ==> "{\"code\":\""+ 变量 +"\"}"
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
